import { RosterInterface } from '@/api/interface/personnel/roster/roster';
import { BasicForm } from '@/components/ProForm/interface';
import { PublicInterface } from '@/interface/public';

export function useAddForm(proTableBox: PublicInterface.ProTableValue, proFormBox: PublicInterface.ProFormValue) {
  let addForm = reactive<BasicForm<RosterInterface.ReqRosterSave>>({
    form: {
      entryDept: '',
      introduceName: '',
      introduceRelation: '',
      entryJob: '',
      id: null,
      type: '',
      idScope: '',
      flagNormal: null,
      name: '',
      gender: null,
      birthday: '',
      nativePlace: '',
      nation: '',
      weight: null,
      height: null,
      politicsStatus: '',
      education: '',
      marriageStatus: '',
      identityNumber: '',
      identityEnd: '',
      identityAddress: '',
      phoneNumber: '',
      residentialAddress: '',
      urgencyName: '',
      urgencyRelation: '',
      urgencyPhone: '',
      recruitSource: '',
      illnessName: '',
      entryDate: '',
      longitude: null,
      latitude: null,
      normalDate: '',
      remark: ''
    },
    title: '',
    minWidth: 660,
    labelWidth: [100, 120],
    visible: false,
    formAttrs: {
      labelWidth: '120px'
    },
    disabledPropList: ['identityNumber', 'type'],
    hidePropList: ['otherRecruitSource'],
    transitionArr: [
      {
        oldField: 'nation',
        newField: 'nation'
      },
      {
        oldField: 'politicsStatus',
        newField: 'politicsStatus'
      },
      {
        oldField: 'education',
        newField: 'education'
      },
      {
        oldField: 'marriageStatus',
        newField: 'marriageStatus'
      },
      {
        oldField: 'relation',
        newField: 'urgencyRelation'
      },
      {
        oldField: 'recruitSource',
        newField: 'recruitSource'
      },
      {
        oldField: 'relation',
        newField: 'introduceRelation'
      },
      {
        oldField: 'employeeType',
        newField: 'type'
      },
      {
        oldField: 'gender',
        newField: 'gender'
      }
    ],
    options: [
      {
        type: 'input',
        label: 'ID',
        prop: 'id',
        hide: true
      },
      {
        type: 'select',
        hide: false,
        prop: 'type',
        label: '员工类型',
        rules: [
          {
            required: true,
            message: '员工类型',
            trigger: 'change'
          }
        ],
        attrs: {
          disabled: false
        },
        children: [],
        changeMethod: () => {
          // const flag = addForm.form.type === "正式工";
          // changeFormItemStatus(addForm, ["flagNormal"], flag, "hide");
          // if (!flag) {
          //   if (!addForm.form.flagNormal) {
          //     addForm.form.flagNormal = 0;
          //   }
          //   // 如果转正 显示转正日期
          //   if (addForm.form.flagNormal === 1) {
          //     changeFormItemStatus(addForm, ["normalDate"], false, "hide");
          //   }
          // }
        }
      },

      {
        type: 'input',
        hide: false,
        prop: 'name',
        label: '姓名',
        rules: [
          {
            required: true,
            message: '姓名',
            trigger: 'blur'
          }
        ]
      },
      {
        type: 'input',
        hide: false,
        prop: 'identityNumber',
        attrs: {
          disabled: false
        },
        label: '身份证号码',
        rules: [
          {
            required: true,
            message: '身份证号码',
            trigger: 'blur'
          }
        ],
        blurFunction: form => {
          const { identityNumber, nativePlace, identityAddress } = form || {};
          const { cityName, countyName, provinceName }: any = getAreaByIdCard(identityNumber);

          // 填充部分身份证地址
          if (!identityAddress && provinceName && cityName && countyName) {
            form.identityAddress = `${provinceName}${cityName}${countyName}`;
          }

          addForm.form.gender = getSexText(identityNumber);

          // 填充籍贯
          if (!nativePlace) {
            const index = provinceName.indexOf('省');
            form.nativePlace = index === -1 ? provinceName : provinceName.substring(0, index);
          }

          // 填充出生日期
          if (identityNumber.length > 16) {
            // 年
            const year = identityNumber.substring(6, 10);
            // 月
            const month = identityNumber.substring(10, 12);
            // 日
            const day = identityNumber.substring(12, 14);
            form.birthday = `${year}-${month}-${day}`;
          }
        }
      },
      {
        type: 'input',
        hide: false,
        prop: 'phoneNumber',
        label: '手机号',
        rules: [
          {
            required: true,
            message: '手机号',
            trigger: 'blur'
          }
        ]
      },
      {
        type: 'input',
        hide: false,
        prop: 'weight',
        label: '体重',
        attrs: {
          type: 'number',
          placeholder: '请输入体重(KG)'
        }
        // rules: [
        //   {
        //     required: true,
        //     message: "体重",
        //     trigger: "blur"
        //   }
        // ]
      },
      {
        type: 'input',
        hide: false,
        prop: 'height',
        label: '身高',
        attrs: {
          type: 'number',
          placeholder: '请输入身高(cm)'
        }
        // rules: [
        //   {
        //     required: true,
        //     message: "身高",
        //     trigger: "blur"
        //   }
        // ]
      },
      {
        type: 'select',
        hide: false,
        prop: 'nation',
        label: '民族',
        children: [],
        defaultValue: '汉族',
        rules: [
          {
            required: true,
            message: '民族',
            trigger: 'blur'
          }
        ]
      },
      {
        type: 'select',
        hide: false,
        prop: 'politicsStatus',
        label: '政治面貌',
        defaultValue: '群众',
        rules: [
          {
            required: true,
            message: '政治面貌',
            trigger: 'change'
          }
        ],
        children: []
      },
      {
        type: 'select',
        hide: false,
        prop: 'education',
        label: '学历',
        defaultValue: '小学',
        rules: [
          {
            required: true,
            message: '学历',
            trigger: 'change'
          }
        ],
        children: []
      },
      {
        type: 'select',
        hide: false,
        prop: 'marriageStatus',
        defaultValue: '已婚',
        label: '婚姻状况',
        rules: [
          {
            required: true,
            message: '婚姻状况',
            trigger: 'change'
          }
        ],
        children: []
      },
      {
        type: 'select',
        hide: false,
        prop: 'gender',
        label: '性别',
        rules: [
          {
            required: true,
            message: '性别',
            trigger: 'change'
          }
        ],
        children: []
      },
      {
        type: 'date-picker',
        hide: false,
        prop: 'birthday',
        label: '出生日期',
        attrs: {
          type: 'date',
          valueFormat: BasicKey.DATE_FORMAT.YYYY_MM_DD
        },
        rules: [
          {
            required: true,
            message: '出生日期',
            trigger: 'blur'
          }
        ]
      },
      {
        type: 'input',
        hide: false,
        prop: 'nativePlace',
        label: '籍贯',
        rules: [
          {
            required: true,
            message: '籍贯',
            trigger: 'blur'
          }
        ]
      },
      {
        type: 'input',
        hide: false,
        prop: 'identityAddress',
        label: '身份证地址',
        rules: [
          {
            required: true,
            message: '身份证地址',
            trigger: 'blur'
          }
        ]
      },

      {
        type: 'select',
        label: '身份证有效期范围',
        hide: false,
        prop: 'idScope',
        defaultValue: '有截止日',
        children: statusValid,
        attrs: {
          disabled: false,
          clearable: true
        },
        changeMethod: () => {
          nextTick(() => {
            formChangeItemStatus(addForm, ['identityEnd'], addForm.form.idScope != '有截止日', 'hide');
            if (addForm.form.idScope != '有截止日') {
              addForm.form.identityEnd = '2099-12-31';
            } else {
              addForm.form.identityEnd = '';
            }
          });
        }
        // rules: [
        //   {
        //     required: true,
        //     message: "有效期范围",
        //     trigger: "blur"
        //   }
        // ]
      },
      {
        type: 'date-picker',
        label: '有效期',
        hide: false,
        prop: 'identityEnd',
        defaultValue: '',
        attrs: {
          type: 'date',
          disabled: false,
          clearable: true,
          valueFormat: BasicKey.DATE_FORMAT.YYYY_MM_DD
        }
        // rules: [
        //   {
        //     required: true,
        //     message: "有效期",
        //     trigger: "blur"
        //   }
        // ]
      },

      {
        type: 'input',
        hide: false,
        prop: 'residentialAddress',
        label: '当前居住地址'
        // rules: [
        //   {
        //     required: true,
        //     message: "当前居住地址",
        //     trigger: "blur"
        //   }
        // ]
      },
      {
        type: 'input',
        hide: false,
        prop: 'urgencyName',
        label: '紧急联系人姓名',
        rules: [
          {
            required: true,
            message: '紧急联系人姓名',
            trigger: 'blur'
          }
        ]
      },
      {
        type: 'select',
        hide: false,
        prop: 'urgencyRelation',
        label: '紧急联系人关系',
        rules: [
          {
            required: true,
            message: '紧急联系人关系',
            trigger: 'blur'
          }
        ],
        children: []
      },
      {
        type: 'input',
        hide: false,
        prop: 'urgencyPhone',
        label: '紧急联系人号码',
        rules: [
          {
            required: true,
            message: '紧急联系人号码',
            trigger: 'blur'
          }
        ]
      },
      {
        type: 'select',
        hide: false,
        prop: 'recruitSource',
        label: '应聘来源',
        children: [],
        changeMethod: () => {
          const flag = addForm.form.recruitSource === '其他';
          formChangeItemStatus(addForm, ['otherRecruitSource'], !flag, 'hide');
          addForm.form.otherRecruitSource = '';
        },
        rules: [
          {
            required: true,
            message: '应聘来源',
            trigger: 'change'
          }
        ]
      },
      {
        type: 'input',
        hide: true,
        prop: 'otherRecruitSource',
        label: '其他应聘来源',
        rules: [
          {
            required: true,
            message: '其他应聘来源',
            trigger: 'blur'
          }
        ]
      },

      {
        type: 'input',
        hide: false,
        prop: 'introduceName',
        label: '介绍人姓名',
        rules: [
          {
            required: false,
            message: '介绍人姓名',
            trigger: 'blur'
          }
        ],
        blurFunction: form => {
          let flag = !!(form.introduceName || form.introduceRelation);
          fromChangeVerifyRole(addForm, ['introduceName', 'introduceRelation'], flag);
        }
      },
      {
        type: 'select',
        hide: false,
        prop: 'introduceRelation',
        label: '介绍人关系',
        children: [],
        rules: [
          {
            required: false,
            message: '介绍人关系',
            trigger: 'blur'
          }
        ],
        changeMethod: form => {
          let flag = !!(form.introduceName || form.introduceRelation);
          fromChangeVerifyRole(addForm, ['introduceName', 'introduceRelation'], flag);
        }
      },
      {
        type: 'input',
        hide: false,
        prop: 'illnessName',
        label: '疾病名称'
      },
      {
        type: 'date-picker',
        hide: false,
        prop: 'normalDate',
        label: '转正日期',
        attrs: {
          type: 'date',
          valueFormat: BasicKey.DATE_FORMAT.YYYY_MM_DD
        }
      },
      {
        type: 'date-picker',
        hide: false,
        prop: 'entryDate',
        label: '入职日期',
        attrs: {
          type: 'date',
          valueFormat: BasicKey.DATE_FORMAT.YYYY_MM_DD
        }
      },

      {
        type: 'input',
        hide: false,
        prop: 'entryDept',
        label: '入职部门',
        attrs: {
          disabled: true
        }
      },
      {
        type: 'input',
        hide: false,
        prop: 'entryJob',
        label: '入职岗位',
        attrs: {
          disabled: true
        }
      },
      {
        type: 'input',
        hide: false,
        prop: 'remark',
        label: 'remark',
        attrs: {
          style: {
            width: '520px'
          }
        }
      }
    ]
  });

  /**
   * @description: 保存表单数据
   * @param {OperatorInfo} form
   * @return {*}
   */
  const saveAddForm = async () => {
    return new Promise((resolve, reject) => {
      proFormBox.value.submitForm().then((form: RosterInterface.ReqRosterSave) => {
        const params = { ...form };
        if (params.recruitSource === '其他') {
          params.recruitSource = params.otherRecruitSource;
        }
        // searchAddr(params.identityAddress, false).then(address => {
        //   // 如果地址有经纬度那么更新经纬度信息
        //   if (address) {
        //     Object.assign(params, address);
        //   }
        console.log(params, 'params');

        rosterSave(params)
          .then((backParams: any) => {
            addForm.visible = false;
            elMessage();
            proTableBox.value?.getTableList();
            resolve({ id: backParams.id });
          })
          .catch(() => reject(false));
        // });
      });
    });
  };

  /** 从身份证获取性别 */
  const getSexText = idCardNo => {
    const gender = idCardNo.substr(-2, 1);
    if (gender % 2 === 1) {
      return 1;
    }
    return 0;
  };

  return { addForm, saveAddForm };
}
